package com.example.module_home.View.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebSettings;


import com.blankj.utilcode.util.FragmentUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.module_base.Base.BaseFragment;
import com.example.module_base.Controll.MyToolBar;
import com.example.module_base.Net.HttpClient;
import com.example.module_home.Adapter.AllOperationAdapter;
import com.example.module_home.Base.BaseStackFragment;
import com.example.module_home.Entity.CodeInfo;
import com.example.module_home.Entity.UserOperationInfo;
import com.example.module_home.Module.CodeBank.CodeBankManager;
import com.example.module_home.Public.Constant;
import com.example.module_home.R;
import com.example.module_home.Utils.DialogUtil;
import com.example.module_home.View.HomeCodeActivity;
import com.example.module_home.databinding.HomeFragmentCodewebBinding;

import java.util.List;

import br.tiagohm.markdownview.css.InternalStyleSheet;
import br.tiagohm.markdownview.css.styles.Github;

/**
 * Created by 陈煜 on 2018/4/28.
 */

public class HMCodeWebFragment extends BaseStackFragment<HomeFragmentCodewebBinding>{

    private int mCodeID;
    private InternalStyleSheet mStyle;
    private HomeFragmentCodewebBinding mBinding;
    private String mPath;
    private List<UserOperationInfo> mInfoList;

    @Override
    public int setlayoutid() {
        return R.layout.home_fragment_codeweb;
    }

    @Override
    public void init(final HomeFragmentCodewebBinding binding) {
        this.mBinding = binding;
        getMainActivityInfo();
        setupToolbar();
        setupWebView();
        setupRightMenu();
        loadWebCode(mCodeID);
    }


    private void loadWebCode(int codeId) {
        CodeBankManager.getInstance(mContext).getCodeInfo(codeId, DialogUtil.showLoadingDialog(mContext),new CodeBankManager.CodeBankBack<CodeInfo>() {
            @Override
            public void successful(final CodeInfo info) {
                mBinding.setCodeInfo(info);
//                if(mWebViewClient ==null){
//                    mWebViewClient = new MyWebViewClient();
//                }
//                mWebViewClient.setCode(info.getCodeData());
//                bingding.webCode.setWebViewClient(mWebViewClient);
//                bingding.webCode.setWebViewClient(new WebViewClient(){
//                    @Override
//                    public void onPageFinished(WebView view, String url) {
//                        super.onPageFinished(view, url);
//                        openEdit(info.getCodeData());
//                    }
//                });
//                openEdit(info.getCodeData());
//                bingding.setCodeInfo(info);
//                InternalStyleSheet style = new Github();
//                bingding.webCode.addStyleSheet(style);
//                bingding.webCode.loadMarkdown(replaceSpace(info.getCode()));
            }

            @Override
            public void failed(int code, String e) {

            }
        });
    }


    private void setupWebView() {
        WebSettings settings = mBinding.webCode.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setSupportZoom(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        mPath = "file:///android_asset/tinyMce/edit.html";
        mBinding.webCode.loadUrl(mPath);
    }

    private void setupRightMenu() {
        CodeBankManager.getInstance(mContext).getAllOperations(DialogUtil.showLoadingDialog(mContext), new CodeBankManager.CodeBankBack<List<UserOperationInfo>>() {
            @Override
            public void successful(List<UserOperationInfo> infoList) {
                mInfoList = infoList;
                LinearLayoutManager manager = new LinearLayoutManager(mContext);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                AllOperationAdapter adapter = new AllOperationAdapter(infoList);
                mBinding.rightMenu.recyAlloperation.setLayoutManager(manager);
                mBinding.rightMenu.recyAlloperation.setAdapter(adapter);
                ViewingCode(adapter);
            }

            @Override
            public void failed(int code, String e) {
                toast(e);
            }
        });
    }

    private void ViewingCode(AllOperationAdapter adapter) {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent intent = new Intent(HomeMainActivity.this, HomeCodeActivity.class);
//                intent.putExtra(Constant.CODE_TITLE,mInfoList.get(position).getTitle());
//                startActivity(intent);
                loadWebCode(mInfoList.get(position).getKindID());
                mBinding.drawerLayout.closeDrawer(Gravity.END);
            }
        });
    }

    private void getMainActivityInfo() {
        mCodeID = getArguments().getInt(Constant.CODE_ID);
    }


    private void setupToolbar() {
        mBinding.lineToolbar.setNavigationIcon(com.example.module_base.R.drawable.ic_arrow_back_black_24dp);
        mBinding.lineToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackClick();
            }
        });
    }

    @Override
    public boolean onBackClick() {
        return true;
    }
}
